/*
 * Copyright (C) 2021 Huawei Device Co., Ltd.
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package org.yczbj.ycvideoplayer.adapter;

import ohos.agp.components.BaseItemProvider;
import ohos.agp.components.Component;
import ohos.agp.components.ComponentContainer;
import ohos.agp.components.Image;
import ohos.agp.components.LayoutScatter;
import ohos.agp.components.StackLayout;
import ohos.agp.components.Text;

import com.yc.video.config.VideoInfoBean;
import com.yc.video.ui.view.CustomPrepareView;

import org.yczbj.ycvideoplayer.ConstantVideo;
import org.yczbj.ycvideoplayer.ResourceTable;
import org.yczbj.ycvideoplayer.fraction.OnItemChildClickListener;
import org.yczbj.ycvideoplayer.fraction.OnItemClickListener;
import org.yczbj.ycvideoplayer.utils.ImageLoader;

import java.util.List;

/**
 * 适配器
 *
 * @since 2021-05-10
 */
public class VideoRecyclerViewAdapter extends BaseItemProvider {
    private List<VideoInfoBean> videoList;
    private OnItemChildClickListener mOnItemChildClickListener;
    private OnItemClickListener mOnItemClickListener;

    /**
     * 构造函数
     */
    public VideoRecyclerViewAdapter() {
    }

    /**
     * 构造函数
     *
     * @param videoList 视频列表
     */
    public VideoRecyclerViewAdapter(List<VideoInfoBean> videoList) {
        this.videoList = videoList;
    }

    // 注意这里有个问题,如果这里给的是-1会无限滑动,会出现ANR情况,所以给个固定数量
    @Override
    public int getCount() {
        return videoList == null ? ConstantVideo.getVideoList().size() : videoList.size();
    }

    @Override
    public VideoInfoBean getItem(int i) {
        return videoList.get(i);
    }

    @Override
    public long getItemId(int i) {
        return i;
    }

    // 重写该方法即可避免复用布局出现错乱问题
    @Override
    public int getItemComponentType(int position) {
        return position;
    }

    // 重写改方法 布局类型的数量
    @Override
    public int getComponentTypeCount() {
        return 1;
    }

    /**
     * 设置数据
     *
     * @param list
     */
    public void setData(List<VideoInfoBean> list) {
        this.videoList = list;
    }

    @Override
    public Component getComponent(int position, Component component, ComponentContainer componentContainer) {
        Holder holder;
        if (component == null) {
            component = LayoutScatter.getInstance(componentContainer.getContext()).parse(
                ResourceTable.Layout_item_video, null, false);
            holder = new Holder(component);
            component.setTag(holder);
        } else {
            holder = (Holder) component.getTag();
        }
        holder.mPosition = position;
        holder.setData();
        return component;
    }

    /**
     * Holder
     *
     * @since 2021-05-12
     */
    public class Holder implements Component.ClickedListener {
        /**
         * 角标值
         */
        public int mPosition;
        /**
         * 布局
         */
        public StackLayout mPlayerContainer;
        /**
         * 标题
         */
        public Text mTitle;
        /**
         * Thumb
         */
        public Image mThumb;
        /**
         * 准备视图
         */
        public CustomPrepareView mPrepareView;

        /**
         * 构造函数
         *
         * @param component component
         */
        public Holder(Component component) {
            mPlayerContainer = (StackLayout) component.findComponentById(ResourceTable.Id_player_container);
            mTitle = (Text) component.findComponentById(ResourceTable.Id_tv_title);
            mPrepareView = (CustomPrepareView) component.findComponentById(ResourceTable.Id_prepare_view);
            mThumb = mPrepareView.getThumb();
            if (mOnItemChildClickListener != null) {
                mPlayerContainer.setClickedListener(this);
            }
            if (mOnItemClickListener != null) {
                component.setClickedListener(this);
            }

            // 通过tag将ViewHolder和itemView绑定
            component.setTag(this);
        }

        /**
         * 设置数据
         */
        public void setData() {
            VideoInfoBean videoBean = videoList.get(mPosition);
            ImageLoader.loadImage(mThumb.getContext(), videoBean.getCover(), mThumb);
            mTitle.setText(videoList.get(mPosition).getTitle());
        }

        @Override
        public void onClick(Component component) {
            if (component.getId() == ResourceTable.Id_player_container) {
                if (mOnItemChildClickListener != null) {
                    mOnItemChildClickListener.onItemChildClick(mPosition);
                }
            } else {
                if (mOnItemClickListener != null) {
                    mOnItemClickListener.onItemClick(mPosition);
                }
            }
        }
    }

    public void setOnItemChildClickListener(OnItemChildClickListener onItemChildClickListener) {
        mOnItemChildClickListener = onItemChildClickListener;
    }

    public void setOnItemClickListener(OnItemClickListener onItemClickListener) {
        mOnItemClickListener = onItemClickListener;
    }
}
